package com.hex.ds.hdtp.core.web.dqc.controller;

import com.hex.bigdata.hbda.annotation.Log;
import com.hex.ds.hdtp.core.app.common.page.dto.PageDto;
import com.hex.ds.hdtp.core.app.dqc.dto.request.DqcTaskRequestDto;
import com.hex.ds.hdtp.core.app.dqc.service.IDqcTaskService;
import com.hex.ds.hdtp.core.web.common.page.PageVo;
import com.hex.ds.hdtp.core.web.common.page.converter.IPageVoAndPageDtoConverter;
import com.hex.ds.hdtp.core.web.dqc.converter.IDqcTaskVoConverter;
import com.hex.ds.hdtp.core.web.dqc.vo.DqcTaskRulesVo;
import com.hex.ds.hdtp.core.web.dqc.vo.DqcTaskVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.sql.SQLException;
import java.util.List;

@Slf4j
@RestController
@Api(tags = "定制化-DQC校验：任务")
@RequestMapping("/api/dqc/task")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DqcTaskController {

    private final IDqcTaskService dqcTaskService;

    private final IDqcTaskVoConverter converter;

    private final IPageVoAndPageDtoConverter pageVoAndPageDtoConverter;

    @Log("新增DQC任务")
    @PostMapping
    @ApiOperation("新增Dqc任务")
    public ResponseEntity add(@RequestBody DqcTaskVo dqcTaskVo) {
        log.info("进入【DqcTaskController.add】方法: 参数: " + dqcTaskVo.toString());
        return ResponseEntity.ok(dqcTaskService.addWithBackId(converter.toDqcTaskRequestDto(dqcTaskVo)));
    }

    @Log("DQC任务-分页查询")
    @GetMapping
    @ApiOperation("Dqc任务-分页查询")
    public ResponseEntity queryListByPage(DqcTaskVo dqcTaskVo, PageVo pageVo) {
        log.debug("进入【DqcTaskController.queryListByPage】方法,参数：{}", dqcTaskVo.toString());
        DqcTaskRequestDto requestDto = converter.toDqcTaskRequestDto(dqcTaskVo);
        final PageDto pageDto = pageVoAndPageDtoConverter.toPageDto(pageVo);
        return ResponseEntity.ok(dqcTaskService.queryListByPage(requestDto, pageDto));
    }

    @Log("Dqc任务-更新")
    @PutMapping
    @ApiOperation("Dqc任务-更新")
    public ResponseEntity update(@RequestBody DqcTaskVo dqcTaskVo) {
        log.info("进入到【DqcTaskController.update】方法,主键：{}", dqcTaskVo.getPkId());
        dqcTaskService.modifyById(converter.toDqcTaskRequestDto(dqcTaskVo));
        return ResponseEntity.ok("更新成功");
    }

    @Log("Dqc任务-更新规则")
    @PostMapping(value = {"/updateRules"})
    @ApiOperation("Dqc任务-更新规则")
    public ResponseEntity updateRules(@RequestBody DqcTaskRulesVo dqcTaskRulesVo) {
        log.info("进入到【DqcTaskController.updateRules】方法,主键：{}", dqcTaskRulesVo.getPkId());
        dqcTaskService.modifyRulesById(converter.toDqcTaskRequestDto(dqcTaskRulesVo));
        return ResponseEntity.ok("更新成功");
    }

    @Log("Dqc任务-批量删除")
    @DeleteMapping(value = {"/{pkIds}"})
    @ApiOperation("Dqc任务-批量删除")
    public ResponseEntity delete(@PathVariable("pkIds") @NotNull List<String> pkIds) {
        log.info("进入【DqcTaskController.delete】方法,参数：" + pkIds);
        dqcTaskService.batchRemoveById(pkIds);
        return ResponseEntity.ok("删除成功");
    }

    @Log("Dqc任务试跑")
    @GetMapping("/tryrun/{pkId}")
    @ApiOperation("Dqc任务-任务试跑")
    public ResponseEntity tryRun(@PathVariable("pkId") @NotNull String pkId) throws SQLException {
        log.info("进入【DqcTaskController.tryRun】方法,参数：" + pkId);
        String result = dqcTaskService.tryRun(pkId);
        return ResponseEntity.ok(result);
    }


}
